package recite.word.client.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import recite.word.client.dto.skin.SetSkinDTO;
import recite.word.client.service.ISkinService;
import recite.word.client.vo.skin.GetSkinAllVO;
import recite.word.mybatis.auto.dao.SkinMapper;
import recite.word.mybatis.auto.dao.UserMapper;
import recite.word.mybatis.auto.model.Skin;
import recite.word.mybatis.auto.model.User;
import recite.word.service.base.common.enums.DeleteEnum;
import recite.word.service.base.common.request.BaseRequest;
import recite.word.service.base.common.threadLocal.UserContent;
import recite.word.service.base.util.common.Asserts;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SkinServiceImpl extends ServiceImpl<SkinMapper, Skin> implements ISkinService {
    @Resource
    private UserMapper userMapper;

    /**
     * 获取全部主题颜色
     * @return
     */
    @Override
    public List<GetSkinAllVO> setDayComplete() {
        return this.list(new LambdaQueryWrapper<Skin>().eq(Skin::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode())).stream().map(item -> {
            GetSkinAllVO skinAllVO = new GetSkinAllVO();
            skinAllVO.setSkinId(item.getId());
            skinAllVO.setDesc(item.getDesc());
            skinAllVO.setThemeColor(item.getThemeColor());
            skinAllVO.setThemeColorLess(item.getThemeColorLess());
            return skinAllVO;
        }).collect(Collectors.toList());
    }

    /**
     * 获取默认主题颜色
     * @return
     */
    @Override
    public GetSkinAllVO getDefault() {
        Skin skin = this.getOne(new LambdaQueryWrapper<Skin>().eq(Skin::getIsDefault, true).eq(Skin::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode()));
        GetSkinAllVO ans = new GetSkinAllVO();
        ans.setSkinId(skin.getId());
        ans.setDesc(skin.getDesc());
        ans.setThemeColor(skin.getThemeColor());
        ans.setThemeColorLess(skin.getThemeColorLess());
        return ans;
    }

    /**
     * 设置主题颜色
     * @param dto
     */
    @Override
    public void setSkin(BaseRequest<SetSkinDTO> dto) {
        SetSkinDTO data = dto.getData();
        Asserts.isTrue(ObjectUtil.isNotNull(data.getSkinId()),"主题id为空");
        int b = userMapper.update(null, new LambdaUpdateWrapper<User>()
                .eq(User::getId, UserContent.get())
                .eq(User::getDeleteStatus, DeleteEnum.NOT_DELETED.getCode())
                .set(User::getSkinId, data.getSkinId()));
        Asserts.isTrue(b == 1, "设置主题失败");
    }

    /**
     * 获取当前用户主题颜色
     * @return
     */
    @Override
    public GetSkinAllVO getSkin() {
        User user = userMapper.selectById(UserContent.get());
        // 如果用户还没设置皮肤id,就用默认主题id
        if (ObjectUtil.isNull(user.getSkinId())) {
            return getDefault();
        }
        Skin skin = this.getById(user.getSkinId());
        GetSkinAllVO ans = new GetSkinAllVO();
        ans.setSkinId(skin.getId());
        ans.setDesc(skin.getDesc());
        ans.setThemeColorLess(skin.getThemeColorLess());
        ans.setThemeColor(skin.getThemeColor());
        return ans;
    }
}
